package com.ltxtiyu.video.consumer;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.ltxtiyu.video.bean.VideoInfoBean;
import com.ltxtiyu.video.cacheDao.VideoCacheDao;
import com.ltxtiyu.video.common.CodeTypeConstant;
import com.ltxtiyu.video.common.ConfigConstant;
import com.ltxtiyu.video.entity.Video;
import com.ltxtiyu.video.tool.PushTool;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;

/**
 * 消息推送
 */
@Component
public class PushMessageConsumer {
    public static final Logger LOGGER = LoggerFactory.getLogger(PushMessageConsumer.class);
    @Autowired
    VideoCacheDao videoCacheDao;
    @Autowired
    AmqpTemplate amqpTemplate;

    /**
     * app推送消息队列, 请严格遵守map的参数名和类型
     * @param map
     */
    @RabbitListener(queues=ConfigConstant.MESSAGE_NAME_APP_PUSH_MEASSGE)
    public void appPushMessage(Map<String,Object> map){
        // 列推app 多个token用英文逗号分隔
        // 推送安卓
        try {
            // 获取参数
            String deviceTokens = (String) map.get("deviceTokens");
            String content = (String) map.get("content");
            Map<String, String> fields = (Map<String, String>) map.get("fields");
            fields.put("coverUrl","");  // coverUrl,封面默认给""
            if(StringUtils.isNotBlank(fields.get("videoId"))){
                Video video = videoCacheDao.selectByPrimaryKey(Integer.parseInt(fields.get("videoId")));
                if(video.getStatus().intValue() == 1){  // 获取封面url
                    VideoInfoBean videoInfoBean = JSON.parseObject(video.getVideoInfo(), VideoInfoBean.class);
                    if(StringUtils.isNotBlank(videoInfoBean.getCoverUrl()) && StringUtils.isNotBlank(videoInfoBean.getPlayUrl())){
                        fields.put("coverUrl",videoInfoBean.getCoverUrl());
                    }
                }else {
                    LOGGER.info(">>>>>>>>>>>>>>>>>>>>视频没有完整信息,参数:{}",map.toString());
                }
            }
            // 发布动态时@,没有封面url,视频未转码完成, 重新发送一个延时消息30s
            /*if(fields.get("type").equals(String.valueOf(CodeTypeConstant.PUSH_MESSAGE_APP_TYPE_1002))
                    && StringUtils.isBlank(fields.get("coverUrl"))){
                *//*amqpTemplate.convertAndSend(ConfigConstant.MESSAGE_NAME_APP_PUSH_MEASSGE, map, new MessagePostProcessor() {
                    @Override
                    public org.springframework.amqp.core.Message postProcessMessage(org.springframework.amqp.core.Message message) throws AmqpException {
                        //设置延迟消费时间30秒
                        message.getMessageProperties().setDelay(ConfigConstant.MESSAGE_DELAY_MS_UPLOAD_VIDEO);
                        return message;
                    }
                });
                LOGGER.info(">>>>>>>>>>>>>重新发送延迟消息,参数:{}",map.toString());*//*
                // 不重新发了,会一直发送死掉
            }else {*/
            fields.remove("videoId");   // 移除videoId
            PushTool.sendAndroidListcast(
                    deviceTokens,
                    content,
                    fields
            );
            // 推送ios
            PushTool.sendIOSListcast(
                    deviceTokens,
                    content,
                    fields
            );
        } catch (Exception e) {
            LOGGER.info(">>>>>>>>>>>>>>>>app推送消息失败!");
            e.printStackTrace();
        }
    }
}
